<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    zaki<>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_plan_reply.class.php 2011-07-07 10:02:58 $
 * @Show Note: 
 */
Class zy_plan_reply extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = PAGENO;
		
		/**
		 *	Construct this module class.
		*/
		public function zy_plan_reply()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="zy_plan_reply.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("zy_plan_reply");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign('sell_type_arr',get_sell_type_arr());//购置性质数组
			$this->smarty->assign('project_arr',get_project_arr());//项目数组
			//$this->smarty->assign('admin_info',get_admin_info());//取得管理员用户数组
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->zy_plan_reply();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
				$col = "plan_reply_id";
				$where = 1;
				//是否展示执行选择批复页面
				$is_jump = isset($_GET['is_jump']) ? intval($_GET['is_jump']) : 0;
				if (1 == $is_jump) {
					$where .= " AND reply_state != 1 " ;
				}
				
				//投资编号检索
				$investment_no = isset($_GET['investment_no']) ? trim($_GET['investment_no']) : '';
				if (!empty($investment_no)) {
					$where .= " AND investment_no like '%{$investment_no}%' ";
				}
				//批复单号检索
				$plan_reply_no = isset($_GET['plan_reply_no']) ? trim($_GET['plan_reply_no']) : '';
				if (!empty($plan_reply_no)) {
					$where .= " AND plan_reply_no like '%{$plan_reply_no}%' ";
				}
				//批复日期
				$plan_reply_start = isset($_GET['plan_reply_start']) && !empty($_GET['plan_reply_start']) ? strtotime(($_GET['plan_reply_start'])) : '';
				//批复日期
				$plan_reply_end = isset($_GET['plan_reply_end']) && !empty($_GET['plan_reply_end']) ? strtotime($_GET['plan_reply_end']) : '';
				
				if (!empty($plan_reply_start)) {
					$where .= " AND plan_reply >= {$plan_reply_start} ";
				}
				
				if (!empty($plan_reply_end)) {
					$plan_reply_end = $plan_reply_end + 86400 - 1 ;//多加一天
					$where .= " AND plan_reply <= {$plan_reply_end} ";
				}
				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				/*$error_sql = end($this->db->getDebugMsg());
				echo $error_sql;*/
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				
				//暂时先展示出30条
				if (1 == $is_jump) {
					$page->set($rowNo,30);
					$numrows = 30;
				}else{
					$page->set($rowNo,$this->pageSize);
					$numrows = $this->pageSize;
				}
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
				//展示数据调  取得展示设备数据
				$v_mc_arr = array();
				$zy_nvestment_db = Factory::getDB('zy_nvestment');
				foreach ($arr as $key=>$value)
				{
					$temp = $zy_nvestment_db->getOne($value['investment_no']);
					$arr[$key]['admin_user_id'] = $temp['admin_user_id'];//取得申请人
					$arr[$key]['plan_notes'] = $temp['plan_notes'];//计划备注
					$v_mc_arr[$value['plan_reply_id']] = $this->get_mc_by_reply_id($value['plan_reply_id']);//取得该批复下的数组
				}
				//print_r($arr);
				//取得
				$this->smarty->assign('arr',$arr);
				$this->smarty->assign('v_mc_arr',$v_mc_arr);//批复过的设备信息
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				if (empty($is_jump)) {
					$this->smarty->assign('act','list');
				}else{
					$this->smarty->assign('act','show_add');
				}
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						$investment_no = isset($_REQUEST['investment_no']) ? trim($_REQUEST['investment_no']) : '';//投资自编号
						$plan_reply_time = isset($_REQUEST['plan_reply_time']) ? intval($_REQUEST['plan_reply_time']) : '';//申请时间
						/*if (empty($investment_no)) {
							alertMsg('-2055');
						}*/
						//声明虚拟设备数组
						$v_mc_arr = array();
						if ($investment_no) {
							//取得虚拟设备
							$v_mc_arr = $this->get_mc_by_investment_no($investment_no);
							//dump($v_mc_arr);
							if (empty($v_mc_arr)) {
								alertMsg('-5014');
							}
							//批复金额处理
							$cost = 0;
							foreach ($v_mc_arr as $value)
							{
								$cost += $value['total'] ;
							}
							//print_r($v_mc_arr);
							$key_arr = array_keys($v_mc_arr);
							$coun = array_pop($key_arr);       
							$this->smarty->assign('coun',$coun);
							$this->smarty->assign('cost',$cost);
						}
						//print_r($v_mc_arr);
						$this->smarty->assign('v_mc_arr',$v_mc_arr);//虚拟设备数组
						//使用模版，展示提交界面
						$this->smarty->assign('investment_no',$investment_no);//计划自编号
						$this->smarty->assign('plan_reply_time',$plan_reply_time);//申请时间
						//使用模版，展示提交界面
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$v_mc_id_arr = $temp['mc_id_arr'];//批复设备数组
						unset($temp['mc_id_arr']);
						$reply_num_arr = $temp['reply_num'];//批复数量数组
						unset($temp['reply_num']);
						$reply_price_arr = $temp['reply_price_arr'];//批复设备总价数组
						unset($temp['reply_price_arr']);
						$insrt = $this->db->insert_zy_plan_reply($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						//如果添加批复成功则添加关联表
						if ($insrt > 0) {
							$this->insert_mc_reply($v_mc_id_arr,$reply_num_arr,$reply_price_arr,$insrt);
						}
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						if ($insrt < 0) {
							alertMsg($error_no, 'index.php?c=zy_plan_reply');
						}else{
							redirectPage('index.php?c=zy_plan_reply');
						}
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
				//取得id，也就是唯一索引
				$plan_reply_id = isset($_REQUEST['plan_reply_id']) ? intval(trim($_REQUEST['plan_reply_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($plan_reply_id);
						
						//取得虚拟设备
						$v_mc_arr = $this->get_mc_by_investment_no($result['investment_no'],1);
						//print_r($v_mc_arr);
						//批复的设备做标记
						$v_mc_arr = $this->get_checked_v_mc($plan_reply_id,$v_mc_arr);
						//print_r($v_mc_arr);
						$key_arr = array_keys($v_mc_arr);
						$coun = array_pop($key_arr); 
						$this->smarty->assign('coun',$coun);
						//print_r($v_mc_arr);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=zy_plan_reply');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						$this->smarty->assign('v_mc_arr',$v_mc_arr);//虚拟设备数组
						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "plan_reply_id=$plan_reply_id";
						$temp = $this->getValue(1);
						$v_mc_id_arr = $temp['mc_id_arr'];//批复设备数组
						unset($temp['mc_id_arr']);
						$reply_num_arr = $temp['reply_num'];//批复数量数组
						unset($temp['reply_num']);
						$reply_price_arr = $temp['reply_price_arr'];//批复设备总价数组
						unset($temp['reply_price_arr']);
						$update = $this->db->update_zy_plan_reply($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						if ($update >= 0) {
							//删除原来的设备批复数据
							$this->del_mc_reply($plan_reply_id);
							//重新插入新数据
							$this->insert_mc_reply($v_mc_id_arr,$reply_num_arr,$reply_price_arr,$plan_reply_id);
						}
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						if ($update < 0) {
							alertMsg($error_no, 'index.php?c=zy_plan_reply');
						}else{
							redirectPage('index.php?c=zy_plan_reply');
						}
						//alertMsg($error_no, 'index.php?c=zy_plan_reply');
				}
		}
		/**
		 *	browse method.
		*/
		public function browse() {
			$plan_reply_id = isset($_REQUEST['plan_reply_id']) ? intval(trim($_REQUEST['plan_reply_id'])) : 0 ;
			//使用模版，展示提交界面
			$result = $this->db->getOne($plan_reply_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_plan_reply');
			}
			//取得投资计划信息
			$zy_nvestment_db = Factory::getDB('zy_nvestment');
			$temp = $zy_nvestment_db->getOne($result['investment_no']);
			$result['admin_user_id'] = $temp['admin_user_id'];//取得申请人
			$result['plan_notes'] = $temp['plan_notes'];//计划备注
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			//取得虚拟设备
			$v_mc_arr = $this->get_mc_by_investment_no($result['investment_no']);
			//print_r($v_mc_arr);
			//批复的设备做标记
			$v_mc_arr = $this->get_checked_v_mc($plan_reply_id,$v_mc_arr);
			//取得批复设备信息
			$zy_plan_reply_mc_arr = $this->get_mc_by_reply_id($plan_reply_id);
			//print_r($zy_plan_reply_mc_arr);
			//分页，通过查询所有来去
			$rowNo = count($zy_plan_reply_mc_arr);
			$page = Factory::getBaseClass('pagination');
			$page->set($rowNo,$this->pageSize);
			$numrows = $this->pageSize;
			$offset  = $page->getSqlMove();
			//unset($zy_plan_reply_mc_arr);
			for ($i=$offset;$i<($numrows+$offset);$i++){
				if(isset($zy_plan_reply_mc_arr[$i]))
					$page_arr[] = $zy_plan_reply_mc_arr[$i]; 
			}
			$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
			$this->smarty->assign('zy_mc_type_arr',$page_arr);//虚拟设备信息
			//print_r($v_mc_arr);
			$this->smarty->assign('v_mc_arr',$v_mc_arr);//虚拟设备数组
			
			$this->smarty->assign('is_show',0);
			$this->smarty->assign('act_title','查看');
			$this->smarty->assign('act','browse');
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$plan_reply_id = isset($_REQUEST['plan_reply_id']) ? intval(trim($_REQUEST['plan_reply_id'])) : 0 ;
				$where = "plan_reply_id=$plan_reply_id";
				//检测该批复是否已经被执行
				$temp = $this->check_reply_is_do($plan_reply_id);
				if (!empty($temp)) {
					alertMsg('-2068');
				}
				$del=$this->db->delete_zy_plan_reply($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if ($del < 0) {
					alertMsg($error_no, 'index.php?c=zy_plan_reply');
				}else{
					redirectPage('index.php?c=zy_plan_reply');
				}
				//alertMsg($error_no, 'index.php?c=zy_plan_reply');
		}
		
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			$temp['plan_reply_no'] = isset($_POST['plan_reply_no']) ? htmlspecialchars(trim($_POST['plan_reply_no'])) : '';//批复单号
			if (empty($temp['plan_reply_no'])) {
				alertMsg('-2071');
			}
			if($isEdit){
				$temp['plan_reply_id'] = $_POST['plan_reply_id'];
				//验证批复单号是否重复
				$re = $this->check_plan_reply_no($temp['plan_reply_no'],$temp['plan_reply_id']);
				if (1 == $re) {
					alertMsg('-2079');
				}
			} else {
				$temp['plan_reply_id'] = "";
				$temp['investment_no'] = isset($_POST['investment_no']) ? htmlspecialchars(trim($_POST['investment_no'])) : '';//投资自编号
				if (empty($temp['investment_no'])) {
					alertMsg('-2055');
				}
				$temp['plan_reply_time'] = isset($_POST['plan_reply_time']) && !empty($_POST['plan_reply_time']) ? strtotime($_POST['plan_reply_time']) : 0;//申请时间
				//验证批复单号是否重复
				$re = $this->check_plan_reply_no($temp['plan_reply_no']);
				if (1 == $re) {
					alertMsg('-2079');
				}
			}
			//补充其他的字段
			
			
			$temp['plan_reply'] = isset($_POST['plan_reply']) && !empty($_POST['plan_reply']) ? strtotime($_POST['plan_reply']) : 0 ;//批复日期
			$temp['plan_reply_price'] = isset($_POST['plan_reply_price']) ? sprintf("%01.2f",$_POST['plan_reply_price']) : 0 ;//批复金额
			$temp['plan_reply_content'] = isset($_POST['plan_reply_content']) ? htmlspecialchars(trim($_POST['plan_reply_content'])) : '';//批复备注
			/*$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';*///附件名称
			//判断是否存在文件上传
			/*if (!empty($file_path['name']) && !empty($temp['plan_reply_no'])) {
				$re = upload_file($_FILES,'file_path',$temp['plan_reply_no']);
				$temp['file_path'] = $re;//获得上传文件名称
			}*/
			$temp['mc_id_arr'] = isset($_POST['mc_id_arr']) ? $_POST['mc_id_arr'] : array();//接收批复设备数组
			if (empty($temp['mc_id_arr'])) {
				alertMsg('-2059');
			}
			
			//循环接受对应虚拟设备ID的批复数量数组
			foreach ($temp['mc_id_arr'] as $value) {
				$reply_num = isset($_POST["reply_num_{$value}"]) && !empty($_POST["reply_num_{$value}"]) ? intval($_POST["reply_num_{$value}"]) : 0;//取得批复机器数量
				if (!empty($reply_num)) {
					$temp['reply_num'][$value] = $reply_num;
					$reply_result = $this->check_plan_no($value,$reply_num);//判断批复数是否超过总申请数
					if (true == $reply_result && empty($isEdit)) {
						alertMsg('-5015');
					}
				}
			}
			//判断如果机器个数与数字个数不对应
			if (count($temp['mc_id_arr']) != count($temp['reply_num'])) {
				alertMsg('-2067');
			}
			//循环接受对应虚拟设备ID的批复设备总价数组
			foreach ($temp['mc_id_arr'] as $value) {
				$reply_price = isset($_POST["reply_price_{$value}"]) && !empty($_POST["reply_price_{$value}"]) ? trim($_POST["reply_price_{$value}"]) : 0;//取得批复总价

				if (!empty($reply_price)) {
					$temp['reply_price_arr'][$value] = $reply_price;
				}
			}
			//判断如果机器个数与批复总价个数不对应
			if (count($temp['mc_id_arr']) != count($temp['reply_price_arr'])) {
				alertMsg('-2067');
			}
			$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
			//判断是否存在文件上传
			if (!empty($file_path['name']) && !empty($temp['plan_reply_no'])) {
				$re = upload_file($_FILES,'file_path',$temp['plan_reply_no']);
				$temp['file_path'] = $re;//获得上传文件名称
			}
			$temp['plan_change_time'] = isset($_POST['plan_change_time']) && !empty($_POST['plan_change_time']) ? strtotime($_POST['plan_change_time']) : 0 ;//调整日期
			$temp['plan_change_price'] = isset($_POST['plan_change_price']) ? sprintf("%01.2f",$_POST['plan_change_price']) : 0 ;//批复调整金额
			$temp['plan_change_no'] = $_POST['plan_change_no'];
 			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'投资计划批复管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'browse' => array('browse', '查看', 16, false),
					'change_state' => array('change_state','状态',32,false),
					'updateFile' => array('updateFile','文件上传',64,false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}
		/**
		 * 改变批复表中的执行状态
		 *
		 */
		function change_state()
		{
			$plan_reply_id = isset($_REQUEST['plan_reply_id']) ? intval(trim($_REQUEST['plan_reply_id'])) : 0 ;
			$where = "plan_reply_id=$plan_reply_id";
			$array = array('reply_state' => 1);
			$update = $this->db->update_zy_plan_reply($array,$where);
			redirectPage('index.php?c=zy_plan_reply');		
		}
		/**
		 * 文件上传
		 *
		 */
		function updateFile()
		{
			$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
			$plan_reply_id = isset($_REQUEST['plan_reply_id']) ? intval(trim($_REQUEST['plan_reply_id'])) : 0 ;
			$error_no = 0;
			//判断是否存在文件上传
			if (!empty($file_path['name'])) {
				$array = array(
					'source_file_name'=>$file_path['name'],
				);
				$file_name = upload_file($_FILES,'file_path',md5($file_path['name'].time()),$_GET['c']);
				$array['file_path'] = $file_name;
				$where = "plan_reply_id = $plan_reply_id ";
				$update = $this->db->update_zy_plan_reply($array,$where);
				$error_no = 0 > $update ?  $update :'-1502';
			}
			if ($error_no == '-1502') {
				echo json_encode(array('code'=>1));exit;
			}else{
				echo json_encode(array('code'=>2));exit;
			}
			
		}
		/**
		 * 检测批复数量总数是否超过申请数量
		 *
		 * @param unknown_type $v_mc_id 虚拟设备ID
		 * @param unknown_type $reply_num 批复数量
		 * @return bool
		 */
		function check_plan_no($v_mc_id,$reply_num)
		{
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			$zy_mc_type_db = Factory::getDB('zy_mc_type');
			//判断批复数量总和是否大于总申请数
			//取得虚拟设备已经被批复的数量
			$plan_reply_num = $zy_plan_reply_mc_db->plan_reply_num_total($v_mc_id);
			//取得虚拟设备信息
			$temp =  $zy_mc_type_db->getOne($v_mc_id);
			if ($temp['plan_no'] - $plan_reply_num < $reply_num) {
				return true;
			}else {
				return false;
			}		
		}
		/**
		 * 根据申请编号取得该下面的虚拟设备信息
		 *
		 * @param unknown_type $investment_no
		 * @return unknown
		 */
		function get_mc_by_investment_no($investment_no,$flag=0)
		{
			$temp = array();
			$zy_mc_type_db = Factory::getDB('zy_mc_type');
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			$arr = $zy_mc_type_db->get_mc_by_investment_no($investment_no);
			//dump($arr);
			if ($arr) {
				foreach ($arr as $key=>$value)
				{
					//取得每个虚拟设备ID被批复的数字 如果批复数量大于等于申请数量 则注销掉不显示 zaki 2011-9-24 修改时设备即使全部批复也要展示
 					$plan_reply_num = $zy_plan_reply_mc_db->plan_reply_num_total($value['v_mc_id']);
					if ($value['plan_no'] >= $plan_reply_num || $flag == 1) {
						$temp[$value['v_mc_id']] = $value;
						$temp[$value['v_mc_id']]['last_no'] = $value['plan_no']-$plan_reply_num;
						$piv_price = sprintf("%01.2f",($value['make_cost'] + $value['manage_cost'] + $value['vat'])/$value['plan_no']);
						$temp[$value['v_mc_id']]['total'] = $piv_price*($value['plan_no']-$plan_reply_num);
					}
					unset($arr[$key]);
				}
			}
			//dump($temp);
			return $temp;
		}
		/**
		 * 根据虚拟设备ID数组取得具体信息
		 *
		 * @param unknown_type $v_mc_id_arr
		 * @return unknown
		 */
		function get_mc_info($v_mc_id_arr)
		{
			$zy_mc_type_db = Factory::getDB('zy_mc_type');
			return $zy_mc_type_db->get_v_mc_info($v_mc_id_arr);
		}
		/**
		 * 添加批复设备关联表表
		 *
		 * @param unknown_type $mc_no_arr  设备ID数组
		 * @param unknown_type $reply_id  批复ID
		 * @param unknown_type $reply_num 批复设备数量对应数组
		 * @return unknown
		 */
		function insert_mc_reply($v_mc_id_arr,$reply_num_arr,$reply_price_arr,$insrt)
		{
			//声明插入数组
			$values = array(
						'plan_reply_id' => $insrt,
						'sell_state'=>0
						);
			//取得虚拟设备数组
			$mc_info = $this->get_mc_info($v_mc_id_arr);
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			foreach ($mc_info as $v) {
				$values['v_mc_id'] = $v['v_mc_id'];//设备ID
				$values['project_id'] = $v['project_id'];//项目ID
				$values['plan_reply_mc_number'] = $reply_num_arr[$v['v_mc_id']];//批复数量
				$values['plan_reply_mc_all_cost'] = $reply_price_arr[$v['v_mc_id']];//批复总价
				$values['is_p_mc'] = $v['is_p_mc'];//是否存在子设备
				$values['investment_no'] = $v['investment_no'];//计划投资编号
				$values['mc_cn_name'] = $v['mc_cn_name'];//中文名称
				$values['zy_plan_replay_no'] = $_POST['zy_plan_replay_no_'.$v['v_mc_id']];//获得批复序号
				$values['change_price'] = $_POST['change_price_'.$v['v_mc_id']];//获得批复序号
				//print_r($_POST);
				//print_r($values);exit;
				$re = $zy_plan_reply_mc_db->insert_zy_plan_reply_mc($values);	
				//如果插入失败 则删除刚插入成功的数据 提示并退出
				if ($re < 0) {
					//删除关联表中成功数据
					$this->del_reply_mc_by_reply_id($insrt);
					//删除批复表中数据
					$this->db->delete_zy_plan_reply($where);
					alertMsg('-1002');
				}		
			}
			return true;	
		}
		/**
		 * 根据批复ID删除管理表中数据
		 *
		 * @param unknown_type $reply_id
		 * @return unknown
		 */
		function del_reply_mc_by_reply_id($reply_id)
		{
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			$where = " plan_reply_id = {$insrt} ";		
			return $zy_plan_reply_mc_db->delete_zy_plan_reply_mc($where);
		}
		/**
		 *给已经批复过的设备做标识
		 *
		 * @param unknown_type $reply_id 批复ID
		 * @param unknown_type $v_mc_arr 设备数组
		 * @return unknown
		 */
		function get_checked_v_mc($reply_id,$v_mc_arr)
		{
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			//根据批复ID取得已经被选中的数据
			$temp = $zy_plan_reply_mc_db->get_checked_v_mc($reply_id);
			if (empty($temp)) {
					alertMsg('-1000');
			}
			//给批复过的加标识
			foreach ($v_mc_arr as $key=>$value){
				if ($temp[$value['v_mc_id']]) {
					$v_mc_arr[$key]['is_reply']  = 1;
					$v_mc_arr[$key]['plan_reply_mc_number']  = $temp[$value['v_mc_id']]['plan_reply_mc_number'];
					$v_mc_arr[$key]['plan_reply_mc_all_cost']  = $temp[$value['v_mc_id']]['plan_reply_mc_all_cost'];
				}else{
					$v_mc_arr[$key]['is_reply']  = 0;
				}
			}
			return $v_mc_arr;
		}
		/**
		 * 根据批复ID删除批复关联表中数据
		 *
		 * @param unknown_type $reply_id
		 * @return unknown
		 */
		function del_mc_reply($reply_id)
		{
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			$where = " plan_reply_id = {$reply_id} ";
			return $zy_plan_reply_mc_db->delete_zy_plan_reply_mc($where);
		}
		/**
		 * 判断该批复是否已经被执行
		 *
		 * @param unknown_type $plan_reply_id
		 * @return unknown
		 */
		function check_reply_is_do($plan_reply_id)
		{
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			$re = $zy_plan_reply_mc_db->check_is_reply_do($plan_reply_id);
			if ($re === false) {
				alertMsg('-1000');
			}
			return $re;
		}
		/**
		 * 取得批复展示的虚拟设备全部信息（虚拟设备表与批复设备连表取得）
		 *
		 * @param unknown_type $reply_id
		 * @return unknown
		 */
		function get_mc_by_reply_id($reply_id)
		{
			$zy_plan_reply_mc_db = Factory::getDB('zy_plan_reply_mc');
			$re = $zy_plan_reply_mc_db->get_reply_mc_info($reply_id);
			return is_array($re) && !empty($re) ? $re : array();
		}
		/**
		 * 检测批复单号是否存在
		 *
		 * @param unknown_type $plan_reply_no
		 * @return unknown
		 */
		function check_plan_reply_no($plan_reply_no,$plan_reply_id = '')
		{
			$zy_plan_reply_db = Factory::getDB('zy_plan_reply');
			$where = " plan_reply_no = '{$plan_reply_no}' ";
			if (!empty($plan_reply_id)) {
				$where .= " AND plan_reply_id != {$plan_reply_id} ";
			}
			$temp = $zy_plan_reply_db->getAll($where,'plan_reply_id',1,0);
			return is_array($temp[0]) ? 1 : 0;
		}
}

?>